package com.socool.sinofaith.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.socool.sinofaith.R;
import com.socool.sinofaith.model.InvestigatesReportResponse.ReportData;
import com.socool.sinofaith.view.ExpandListView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ReportAdapter extends BaseAdapter {

	private Context context;
	private LayoutInflater inflater;
	private List<ReportData> list = new ArrayList<ReportData>();

	public ReportAdapter(Context context) {
		this.context = context;
		inflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public ReportData getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ReportViewHolder mViewHolder = null;
		if (convertView == null) {
			mViewHolder = new ReportViewHolder(context);
			convertView = inflater.inflate(R.layout.adapter_report, parent, false);
			mViewHolder.bindView(convertView);
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ReportViewHolder) convertView.getTag();
		}
		initViewHolder(mViewHolder, position);
		return convertView;
	}

	private void initViewHolder(ReportViewHolder mViewHolder, int position) {
		final ReportData data = getItem(position);
		mViewHolder.tvName.setText(data.getName());
		mViewHolder.adapter.replace(data.getData());
	}

	public void replace(Collection<ReportData> collection) {
		list.clear();
		list.addAll(collection);
		notifyDataSetChanged();
	}

}

class ReportViewHolder {
	TextView tvName;
	ExpandListView elv;
	Report2Adapter adapter;
	Context context;

	public ReportViewHolder(Context context) {
		this.context = context;
		adapter = new Report2Adapter(context);
	}

	public void bindView(View convertView) {
		tvName = (TextView) convertView.findViewById(R.id.tvName);
		elv = (ExpandListView) convertView.findViewById(R.id.elv);
		elv.setAdapter(adapter);
	}

}
